// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports.editor.design.border;

import org.dengues.reports.editor.ReportColorConstants;
import org.dengues.reports.editor.design.utils.BorderUtil;
import org.dengues.reports.editor.design.utils.ColorManager;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-2-20 qiang.zhang $
 * 
 */
public class LineBorder extends BaseBorder {

    private Insets paddingInsets = new Insets();

    /**
     * Qiang.Zhang.Adolf@gmail.com LineBorder constructor comment.
     */
    public LineBorder() {
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.border.BaseBorder#getBorderInsets()
     */
    @Override
    public Insets getBorderInsets() {
        return getTrueBorderInsets();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.border.BaseBorder#getTrueBorderInsets()
     */
    @Override
    public Insets getTrueBorderInsets() {
        int t = 1, b = 1, l = 1, r = 1;

        int style = 0;

        style = getBorderStyle(bottomStyle);
        if (style != 0) {
            b = getBorderWidth(bottomWidth);
        }

        style = getBorderStyle(topStyle);
        if (style != 0) {
            t = getBorderWidth(topWidth);
        }

        style = getBorderStyle(leftStyle);
        if (style != 0) {
            l = getBorderWidth(leftWidth);
        }

        style = getBorderStyle(rightStyle);
        if (style != 0) {
            r = getBorderWidth(rightWidth);
        }

        return new Insets(t, l, b, r);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.reports.editor.design.border.BaseBorder#setPaddingInsets(org.eclipse.draw2d.geometry.Insets)
     */
    @Override
    public void setPaddingInsets(Insets padding) {
        if (padding != null) {
            if (padding.top >= 0) {
                paddingInsets.top = padding.top;
            }
            if (padding.bottom >= 0) {
                paddingInsets.bottom = padding.bottom;
            }
            if (padding.left >= 0) {
                paddingInsets.left = padding.left;
            }
            if (padding.right >= 0) {
                paddingInsets.right = padding.right;
            }
        }

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.Border#getInsets(org.eclipse.draw2d.IFigure)
     */
    public Insets getInsets(IFigure figure) {
        return getTrueBorderInsets().add(paddingInsets);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.Border#paint(org.eclipse.draw2d.IFigure, org.eclipse.draw2d.Graphics,
     * org.eclipse.draw2d.geometry.Insets)
     */
    public void paint(IFigure figure, Graphics g, Insets insets) {
        i_bottom_style = getBorderStyle(bottomStyle);
        i_bottom_width = getBorderWidth(bottomWidth);

        i_top_style = getBorderStyle(topStyle);
        i_top_width = getBorderWidth(topWidth);

        i_left_style = getBorderStyle(leftStyle);
        i_left_width = getBorderWidth(leftWidth);

        i_right_style = getBorderStyle(rightStyle);
        i_right_width = getBorderWidth(rightWidth);

        g.restoreState();

        // draw bottom line
        drawBorder(figure, g, BorderUtil.BOTTOM, i_bottom_style, new int[] { i_top_width, i_bottom_width, i_left_width,
                i_right_width }, bottomColor, insets);

        // draw top line
        drawBorder(figure, g, BorderUtil.TOP, i_top_style,
                new int[] { i_top_width, i_bottom_width, i_left_width, i_right_width }, topColor, insets);

        // draw left line
        drawBorder(figure, g, BorderUtil.LEFT, i_left_style,
                new int[] { i_top_width, i_bottom_width, i_left_width, i_right_width }, leftColor, insets);

        // draw right line
        drawBorder(figure, g, BorderUtil.RIGHT, i_right_style, new int[] { i_top_width, i_bottom_width, i_left_width,
                i_right_width }, rightColor, insets);

    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "drawBorder".
     * 
     * @param figure
     * @param g
     * @param side
     * @param style
     * @param width
     * @param color
     * @param insets
     */
    protected void drawBorder(IFigure figure, Graphics g, int side, int style, int[] width, int color, Insets insets) {
        Rectangle r = figure.getBounds().getCopy().crop(insets);

        if (style != 0) {
            // set ForegroundColor with the given color
            g.setForegroundColor(ColorManager.getColor(color));

            // if the border style is set to "double",
            // draw a double line with the given width and style of "solid"
            if (style == -2) {
                BorderUtil.drawDoubleLine(g, side, width, r);
            }
            // if the border style is set to "solid", "dotted" or "dashed",
            // draw a single line according to the give style and width
            else {
                BorderUtil.drawSingleLine(g, side, style, width, r);
            }
        } else {
            g.setForegroundColor(ReportColorConstants.ShadowLineColor);
            // if the border style is set to none, draw a default dot line in
            // black as default
            BorderUtil.drawDefaultLine(g, side, r);
        }

        g.restoreState();
    }

}
